package com.worksystem.Controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.worksystem.Common.Common;
import com.worksystem.Service.TestService;
import com.worksystem.entity.Subject;
import com.worksystem.entity.Test;
import com.worksystem.entity.User;

@Controller
@RequestMapping("/test")
public class TestController extends Common {
	@Autowired 
	private HttpServletRequest requst;
	
	@Resource
	private TestService testDao;
	
	//test默认主页
	@RequestMapping()
	public ModelAndView defaultmethod(HttpServletRequest request, HttpServletResponse response)  {
		ModelAndView mav=new ModelAndView();
		return mav;
	}
	
	@RequestMapping(value="{id}")
	public ModelAndView getSubjectTests(@PathVariable("id") Integer id ){
		
		Subject subject=testDao.getSubjectById(id);
		ModelAndView mav=new ModelAndView();
		if(subject!=null){
			mav.addObject("subject", subject);
			System.out.println("考试科目名称："+subject.getName());
			List<Test> tests=testDao.getTestsBySubjectId(id);
			System.out.println("试卷题数量："+tests.size());
			mav.addObject("tests", tests);
			mav.setViewName("/test/test");
		}
		
		return mav;
	}
	
	@RequestMapping(value="/ajax/gettests",method = { RequestMethod.POST, RequestMethod.GET },produces = "text/html;charset=UTF-8")
	@ResponseBody
	public Object getTestByJSON(Integer sid){
		Subject subject=testDao.getSubjectById(sid);
		if(subject!=null){
			List<Test> tests=testDao.getTestsBySubjectId(sid);
			String[] parameters={"tests","subject","test"};
			//过滤下参数
			net.sf.json.JSONArray testsjson=processorParameter(tests, parameters);
			String[] subparameters={"classes","tests","grades"};
			net.sf.json.JSONArray subjectjson=processorParameter(subject, subparameters);
			JSONObject jsonarrays=new JSONObject();
			jsonarrays.put("subject",subjectjson);
			jsonarrays.put("tests", testsjson);
			return jsonarrays.toString();
			
		}
		return "false";
	}
		//提交答案
		@RequestMapping(value="/finishtest",method = { RequestMethod.POST, RequestMethod.GET },produces = "text/html;charset=UTF-8")
		@ResponseBody
		public Object postfinishtest(String tests,String time,String subjectid){
			System.out.println("开始提交答案---了----------------------");
			//两种方式获取uid
			Integer uid=1;
			if(requst.getParameter("onlineuser")!=null){
			 uid=Integer.valueOf(requst.getParameter("uid"));
			}
			else{
				HttpSession session=requst.getSession();
				User user=(User) session.getAttribute("onlineuser");
				  uid=user.getUid();
			}
			return testDao.postteststograde(tests, uid, Integer.valueOf(subjectid), time);
				
		}
	
}
